package net.ojbk.service;

import net.ojbk.dao.UserDataDao;
import net.ojbk.po.UserData;

public class UserDataService {
	UserDataService userdataservice;
	UserDataDao userdatadao;
	public UserDataService(){
		userdataservice=new UserDataService();
		userdatadao=new  UserDataDao();
	}
	public int getUserData(String id, UserData ud){
		ud.setId(id);
		if(userdatadao.queryOneRecord(ud)){
			return 1; //获取用户信息成功
		}
		return 0; //获取用户信息失败
	}
	public int setUserData(UserData ud){
		userdatadao.updateOneRecord(ud);
		if(!checkIdcard(ud.getId())){
			return 2;   //身份证格式有误
		}
		if(!checkemail(ud.getEmail())){
			return 3;  //邮箱格式有误
		}
		if(!checktel(ud.getTel()))
			return 4;  //手机号格式有误
		if(ud.getEmail().equals(null)||ud.getTel().equals(null)||ud.getIdcard().equals(null)||ud.getGrade().equals(null)||ud.getCollege().equals(null)||ud.getClas().equals(null)){
			return 5; //用户信息残缺
		}
		return 1; //修改成功
	}
	public boolean checkIdcard(String idcard){   //判断身份证有效性
		boolean flag=false;
		int count=0;
		char a;
		for(int i=0;i<idcard.length();i++){  
			a=idcard.charAt(i);
			if(a<='9'&&a>='0'&&i<17)
				count++;
		    if((i==17)&&((a<='9'&&a>='0')||(a=='x'||a=='X')))
				count++;
		}
		if(count==18) flag=true;
	    	return flag;
	}
	public boolean checkemail(String email){   //判断邮箱有效性
		boolean flag=false;
		char a;
		int m = 0,n=0,count=0;
		for(int i=0;i<email.length();i++){
			a=email.charAt(i);
			if(a=='@'){ 
				m=i;
				count++;
			}
			if(a=='.')
				n=i;
		}
		if(n-m>1) flag=true;
		if(count>1) flag=false;
		if(m<2) flag=false;
		return flag;
	}
	public boolean checktel(String tel){  //判断号码有效性
		boolean flag=false;
		char a;
		int count=0;
		for(int i=0;i<tel.length();i++){
			a=tel.charAt(i);
			if(i==0&&a=='1') count++;
			if(i>1&&a<='9'&&a>='0'){
				count++;
			}
		}
		if(count==10) flag=true;
		return flag;
	}
}
